﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ScoreManage
{
    public partial class TeacherGradeEdit : System.Web.UI.Page
    {
        protected string Cno, Sno;
        protected string strConn = ConfigurationManager.ConnectionStrings["studbConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Cno"] == null)
            {
                Response.Redirect("TeacherGradeManage.aspx?item=1");        // 返回到主页面
            }
            else
            {
                Cno = Request.QueryString["cno"].ToString().Replace("'", "''");
                Sno = Request.QueryString["sno"].ToString().Replace("'", "''");
                if (!IsPostBack)
                {
                    BindData();
                }
            }
        }

        protected void BindData()
        {
            SqlConnection conn = new SqlConnection(strConn);
            conn.Open();
            string strSql = string.Format("SELECT * FROM scview WHERE cno = '{0}' AND sno = '{1}'", Cno, Sno);
            SqlCommand myCommand = new SqlCommand(strSql, conn);
            SqlDataReader dr = myCommand.ExecuteReader();
            if (dr.Read())
            {
                txtCno.Text = dr["cno"].ToString();
                txtCname.Text = dr["cname"].ToString();
                txtSno.Text = dr["sno"].ToString();
                txtSname.Text = dr["sname"].ToString();
                txtScore.Text = dr["score"].ToString();
            }
            else
            {
                Response.Redirect("TeacherGradeManage.aspx?item=1");      // 返回到主页面
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)    // 编辑
        {
            if (Page.IsValid)
            {
                string Cno = txtCno.Text.Trim().Replace("'", "''");
                string Cname = txtCname.Text.Trim().Replace("'", "''");
                string Sno = txtSno.Text.Trim().Replace("'", "''");
                string Sname = txtSname.Text.Trim().Replace("'", "''");
                string Score = txtScore.Text.Trim().Replace("'", "''");

                SqlConnection conn = new SqlConnection(strConn);
                conn.Open();
                string strSql = string.Format("UPDATE student_course SET score = '{0}' WHERE cno='{1}' AND sno='{2}'", Score, Cno, Sno);
                SqlCommand myCommand = new SqlCommand(strSql, conn);
                myCommand.ExecuteNonQuery();                            // 执行命令
                Response.Redirect("TeacherGradeManage.aspx?item=1");         // 返回到主页面
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("TeacherGradeManage.aspx?item=1");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            BindData();
        }
    }
}